define(function(require,exports,module){

	exports.init = function(){
		//获取管理页面第一页数据
		ajax_get_init_list_page(0);
		//点击获取更多
		click_get_more_list();
		//添加增加页面的数据
		ajax_add_insert_html();
	}	
	exports.operate = function(){
		// 编辑加盟信息
		edit_join();
		//点击加盟管理  重载页面
		click_load_list_page();
	}
	exports.win_resize = function(){

	}
	function click_load_list_page(){
		$(".join_us .main_card_content li[target-div=1]").live('click',function(){
			var data = {offset:0}
			$.post(backend_var.ajaxGetJoinListUrl,data,function(e){
				
				$(".join_list").html(e);
			},'html');	
		})
	}
	function ajax_add_insert_html(){
		$.post(backend_var.ajaxAddJoinFormUrl,{},function(e){
			$(".join_add_div").html(e);
			//添加uploadify
			use_uploadify_instead_file();
			//预览logo
			preview_logo_img();
			//加载type
			ajax_load_join_type();
			//添加编辑器
			UE.getEditor('join_info');
			//异步添加
			ajax_add_join();
		},'html');		
	}

	function use_uploadify_instead_file(){
		$('#join_logo_img').uploadify({
	    	'fileTypeExts' : '*.gif; *.jpg; *.png',
	    	'buttonImage':backend_var.uploadify_buttonImg,
	        'swf'      : backend_var.uploadify_url,
	        'formData':{
	        	'asdfa':"adsasdf"
	        },
	        'itemTemplate':'',
	        'removeTimeout' : 100,
	        'uploader' : backend_var.ajaxUploadCookFoodImgUrl,
	        'onUploadSuccess' : function(file, e, response) {
           		var e = $.parseJSON(e);
           		$("input[name='join_logo']").val(e.data['savename']);
           		$("img[name='show_logo_img']").attr("src",backend_var.upload_url+"/"+e.data['savepath']+"/"+e.data['savename']);
           		$(".click_show_logo").show();
				$.Zebra_Dialog('图片上传成功，请填写其他信息', {
				    'type':'information',
				    'title':'提示',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});          		
        	}
	    });
	}
	function ajax_load_join_type(){
		$.post(backend_var.ajaxLoadJoinTypeUrl,{},function(e){
			var str='';
			if(e.status){
				for (var i = e.data.length - 1; i >= 0; i--) {
					str +="<option value="+e.data[i]['join_type_id']+">"+e.data[i]['join_type_name']+"</option>";
				};
				$("select[name='join_type']").append(str);
			}
		},'json');
	}
	function preview_logo_img(){
		$("img[name='show_logo_img']").fancybox({
			aspectRatio:true,
			autoResize:true,
			closeClick:true,
			title:'图片若是太大，请拖动滚动条查看，有待改善！请谅解'
		});
		$(".click_show_logo").live('click',function(){
			$("img[name='show_logo_img']").trigger('click');
		})	
	}
	function ajax_add_join(){
		$(".join_button").die('click');
		$(".join_button").live('click',function(){
			var data = $("form[name='join_form']").serializeArray();
			$.post(backend_var.ajaxAddJoin,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});
				}
			},'json');
		});
	}

	function ajax_get_init_list_page(offset){
		var data = {offset:offset}
		$.post(backend_var.ajaxGetJoinListUrl,data,function(e){
			$(".join_list_more").remove();
			$(".join_list").append(e);
		},'html');	
	}

	function click_get_more_list(){
		$(".join_list_more").live('click',function(){
			var offset = $(this).attr('offset');
			ajax_get_init_list_page(offset);

		});
	}
	function edit_join(){
		$(".each_join_edit").live('click',function(){
			var data = {
				join_id:$(this).parents(".join_each").attr("join-id")
			}
			$.post(backend_var.ajaxGetJoinEditPageUrl,data,function(e){
				$(".join_edit_div").html(e);
				$(".join_us .main_card_content ul li").removeClass("action-target-li");
				$(".join_us .main_card_content ul li[target-div=3]").addClass('action-target-li');
				$(".join_us .main_card_content .target-div .target-son").removeClass("target-div-active");
				$(".join_us .main_card_content .target-div .target-div-3").addClass("target-div-active");

				//添加uploadify
				edit_upload_logo();
				//预览效果
				edit_preview();
				//加载Ue富文本编辑器
				UE.getEditor('join_info_edit');
				//点击保存按钮
				edit_click_save_button();
			},'html');
		})
	}
	function edit_upload_logo(){
		$('#join_eidt_logo_img').uploadify({
	    	'fileTypeExts' : '*.gif; *.jpg; *.png',
	    	'buttonImage':backend_var.uploadify_buttonImg,
	        'swf'      : backend_var.uploadify_url,
	        'formData':{
	        	
	        },
	        'itemTemplate':'',
	        'removeTimeout' : 100,
	        'uploader' : backend_var.ajaxUploadCookFoodImgUrl,
	        'onUploadSuccess' : function(file, e, response) {
           		var e = $.parseJSON(e);
           		$("input[name='join_logo_edit']").val(e.data['savename']);
           		$("img[name='show_logo_img_edit']").attr("src",backend_var.upload_url+"/"+e.data['savepath']+"/"+e.data['savename']);
           		$(".click_show_logo_edit").show();
				$.Zebra_Dialog('图片上传成功，请填写其他信息', {
				    'type':'information',
				    'title':'提示',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});          		
        	}
	    });
	}
	function edit_preview(){
		$("img[name='show_logo_img_edit']").fancybox({
			aspectRatio:true,
			autoResize:true,
			closeClick:true,
			title:'图片若是太大，请拖动滚动条查看，有待改善！请谅解'
		});
		$(".click_show_logo_edit").live('click',function(){
			$("img[name='show_logo_img_edit']").trigger('click');
		})		
	}

	function edit_click_save_button(){
		$(".join_edit_button").die('click');//这里是为了防止触发两次请求，倒是两次弹框
		$(".join_edit_button").live('click',function(){
			var data = $("form[name='join_edit_form']").serializeArray();
			$.post(backend_var.ajaxUpdateJoinUrl,data,function(e){
				if(e.status){
					$.Zebra_Dialog(e.message, {
					    'type':'information',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});
				}else{
					$.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'提示',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});
				}
			},'json');
		});
	}

});